package api;

import com.fasterxml.jackson.databind.ObjectMapper;
import model.User;
import model.UserDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    private ObjectMapper objectMapper = new ObjectMapper();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf8");
        //1.先从请求中获取用户名和密码
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        if (username == null || username.equals("") || password == null || password.equals("")) {
            //用户名密码残缺 登陆失败
            String html = "<h3>登录失败，用户名或密码残缺</h3>";
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write(html);
            return;
        }
        //2.读取数据库 查看用户名和密码是否和数据库中的匹配
        UserDao userDao = new UserDao();
        User user = userDao.selectUserByName(username);
        if (user == null) {
            //用户不存在
            String html = "<h3>用户名或密码错误</h3>";
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write(html);
            return;
        }
        if(!password.equals(user.getPassword())) {
            //密码错误
            String html = "<h3>用户名或密码错误</h3>";
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write(html);
            return;
        }
        //用户名和密码都正确 登陆成功！设置会话
        HttpSession session = req.getSession(true);
        //此处就把用户对象存储到session中 下次用户访问其他页面 可以直接拿到会话，进一步拿到之前的user对象
        session.setAttribute("user",user);
        //返回重定向响应 返回博客列表页
        resp.sendRedirect("blog_list.html");
    }

    //通过这个方法判定用户登陆状态
    //登录成功200 登陆失败403
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //看当前请求是否已经存在会话 并且当前会话是否包含user对象
        HttpSession session = req.getSession(false);
        if (session == null) {
            //会话不存在 未登录
            resp.setStatus(403);
            return;
        }
        User user = (User) session.getAttribute("user");
        if (user == null) {
            //虽然会话对象存在 但是用户对象没有 未登录状态
            resp.setStatus(403);
            return;
        }
        //已经登陆了
        //200是默认状态 此处不写也可以
        resp.setStatus(200);
        resp.setContentType("application/json;charset=utf8");
        user.setPassword("");//避免密码也返回前端
        String respJson = objectMapper.writeValueAsString(user);
        resp.getWriter().write(respJson);
    }
}
